<template>
	<view class="to-attendance-box">
		<view class="course-info">
			<view class="item-top">
				<view class="course-title">{{courseDetail.course.title}}</view>
				<view class="course-time">{{courseDetail.date}}</view>
			</view>
			<view class="item-center">
				<image src="@/static/common/address.png" class="address-icon"></image>
				<view class="address-info" v-if="courseDetail.is_sign == '1'">
					<text class="address-name">{{addressInfo.address}}</text>
					<view class="refresh-address" @click="getSignLocation('refresh')">
						<text :class="courseDetail&&courseDetail.is_sign=='1'?'label active':'label '">重新定位</text>
						<image src="@/static/common/refresh2.png" class="refresh-icon"
							v-if="courseDetail&&courseDetail.is_sign=='1'"></image>
						<image src="@/static/common/refresh1.png" class="refresh-icon" v-else></image>
					</view>
				</view>
				<view class="address-info" v-else>
					<text class="address-name">{{courseDetail.address}}</text>
				</view>
				<view class="clock-in" @click="teacherClockIn">打卡</view>
			</view>
			<view class="item-bottom">
				<view class="bottom-info">
					<text class="label">学校：</text>
					<text class="value">{{courseDetail.school.title}}</text>
				</view>
				<view class="bottom-info">
					<text class="label">班级：</text>
					<text class="value">{{courseDetail.banji.title}}</text>
				</view>
				<view class="bottom-info">
					<text class="label">学期：</text>
					<text class="value">{{courseDetail.term.title}}</text>
				</view>
				<view class="bottom-info">
					<text class="label">班级人数：</text>
					<text class="value">{{courseDetail.banji.student_num}}人</text>
				</view>
				<view class="bottom-info">
					<text class="label">上课地点：</text>
					<text class="value">{{courseDetail.course.place}}</text>
				</view>
				<view class="bottom-info">
					<text class="label">上课时间：</text>
					<text class="value">第{{courseDetail.schedule.num}}节({{courseDetail.schedule.class_time}} )</text>
				</view>
			</view>
		</view>

		<view class="student-list-box" v-if="courseDetail&&courseDetail.student&&courseDetail.student.length>0">
			<view class="student-list">
				<view class="student-item" v-for="(student, index) in courseDetail.student" :key="index">
					<view class="student-name">{{student.name}}</view>
					<view class="student-no">{{student.no}}</view>
					<view class="student-status">
						<view :class="student.status==1?'tag-box success':'tag-box'"
							@click="changeStudentStatus(index,1)">
							正常</view>
						<view :class="student.status==2?'tag-box late':'tag-box'" @click="changeStudentStatus(index,2)">
							迟到
						</view>
						<view :class="student.status==3?'tag-box early':'tag-box'"
							@click="changeStudentStatus(index,3)">早退
						</view>
						<view :class="student.status==4?'tag-box absent':'tag-box'"
							@click="changeStudentStatus(index,4)">
							缺勤</view>
					</view>
				</view>
			</view>
		</view>

		<view class="upload-box">
			<view class="upload-header">
				<view class="upload-title">上传照片</view>
				<view class="upload-desc">支持.png/jpg，10M以内的图片文件</view>
			</view>
			<view class="image-box">
				<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" multiple
					:maxCount="15"></u-upload>
			</view>
		</view>
		<view class="submit-box" @click="submitCourseInfo">提交</view>
		<!-- 填充盒子 -->
		<view style="height: 40rpx;"></view>
		<u-popup mode="center" :show="showPopup" :closeOnClickOverlay="false" :safeAreaInsetBottom="false" round="44rpx"
			bgColor="transparent">
			<view class="popup-content">
				<image class="popup-image" src="@/static/common/success.png" mode="aspectFill"></image>
				<view class="popup-title">{{popupTitle}}</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				courseId: '', // 课程ID
				schoolId: '', // 学校ID
				courseDetail: {}, // 课程详情
				addressInfo: {}, // 地址信息
				longitude: '', // 经度
				latitude: '', // 纬度
				fileList: [], // 上传的图片列表
				showPopup: false, // 是否显示弹窗
				popupTitle: '', // 弹窗标题
			}
		},
		onLoad(options) {
			if (options && options.courseId) {
				this.courseId = options.courseId
				this.getAttendanceDetail();
			}
			if (options && options.schoolId) {
				this.schoolId = options.schoolId
			}
		},
		methods: {
			getSignLocation(typs) {
				let that = this;
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						// 获取当前位置的经纬度
						that.longitude = res.longitude;
						that.latitude = res.latitude;
						// 如果是刷新定位，则重新获取签到信息
						if (typs === 'refresh') {
							that.$Toast("刷新定位成功");
						}
						that.getSignUpByLngLat()
					}
				});
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this.fileList.length;
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.$uploadFilePromise(lists[i].url);
					let item = this.fileList[fileListLen];
					this.fileList.splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result.fullurl,
							img: result.url
						})
					);
					fileListLen++;
				}
			},
			teacherClockIn() {
				if (this.courseDetail.is_sign != '1') {
					return this.showPopupInfo('您已经打过卡啦')
				}
				if (this.addressInfo && !this.addressInfo.isSign) {
					return this.showPopupInfo('不再打卡范围内')
				}
				this.$Api.user.teacherClockIn({
					id: this.courseId,
					lng: this.longitude,
					lat: this.latitude,
					address: this.addressInfo.address,
				}).then(res => {
					if (res.data.is_sign == 1) {
						this.showPopupInfo('打卡成功')
					} else if (res.data.is_sign == 2) {
						this.showPopupInfo('您已经打过卡啦')
					}
					this.getAttendanceDetail();
				})
			},
			getSignUpByLngLat() {
				this.$Api.user.getSignUpByLngLat({
					lng: this.longitude,
					lat: this.latitude,
					school_id: this.schoolId,
				}).then(res => {
					this.addressInfo = res.data;
				})
			},
			getAttendanceDetail() {
				this.$Api.user.getAttendanceDetail({
					id: this.courseId
				}).then(res => {
					this.courseDetail = res.data;
					if (this.courseDetail.image) {
						this.fileList = this.courseDetail.image.split(',').map(item => ({
							url: this.$OssUrl(item, this.courseDetail.oss_url),
							img: item,
						}));
					}
					console.log(this.fileList)
					if (this.courseDetail.is_sign == '1') {
						this.showPopupInfo('记得优先打卡哦')
						this.getSignLocation()
					}
				})
			},
			showPopupInfo(title) {
				this.showPopup = true; // 显示弹窗
				this.popupTitle = title; // 设置弹窗标题
				setTimeout(() => {
					this.showPopup = false; // 2秒后自动关闭弹窗
				}, 2000);
			},
			changeStudentStatus(index, status) {
				// 更新学生状态
				let student = this.courseDetail.student[index];
				if (student.status === status) {
					this.courseDetail.student[index].status = 0; // 重置状态
				} else {
					this.courseDetail.student[index].status = status; // 设置新状态
				}
				this.$forceUpdate()
			},
			submitCourseInfo() {
				if (this.fileList.length === 0) {
					return this.$Toast('请上传课程照片');
				}
				this.$Api.user.submitCourseInfo({
					id: this.courseId,
					image: this.fileList.map(item => item.img).join(','),
					student: this.courseDetail.student
				}).then(res => {
					this.$Toast('提交成功');
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.to-attendance-box {
		min-height: 100vh;
		background: #f5f6fa;
		padding: 20rpx;
		box-sizing: border-box;

		.popup-content {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			width: 312rpx;
			height: 312rpx;
			background: rgba(0, 149, 248, 0.8);
			border-radius: 44rpx;

			.popup-image {
				width: 160rpx;
				height: 140rpx;
			}

			.popup-title {
				font-size: 32rpx;
				color: #FFFFFF;
				margin-top: 20rpx;
			}
		}

		.course-info {
			background: #fff;
			box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.01);
			border-radius: 24rpx;
			padding: 20rpx;
			margin-bottom: 20rpx;

			.item-top {
				display: flex;
				justify-content: space-between;
				padding-bottom: 10rpx;

				.course-title {
					font-size: 30rpx;
					color: #2F1F1A;
					line-height: 42rpx;
					font-weight: bold;
				}

				.course-time {
					font-size: 24rpx;
					color: #333333;
				}
			}

			.item-center {
				display: flex;
				align-items: center;
				padding: 20rpx 0;

				.address-icon {
					width: 32rpx;
					height: 32rpx;
				}

				.address-info {
					flex: 1;
					margin-left: 20rpx;
					margin-right: 20rpx;

					.address-name {
						font-size: 28rpx;
						color: #333333;
					}

					.refresh-address {
						display: flex;
						align-items: center;
						font-size: 24rpx;
						margin-top: 10rpx;

						.label {
							margin-right: 10rpx;
						}

						.active {
							color: #0095F8;
						}

						.refresh-icon {
							width: 20rpx;
							height: 20rpx;
						}
					}
				}

				.clock-in {
					width: 76rpx;
					height: 76rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					background: #0095F8;
					border-radius: 44rpx;
					border: 1rpx solid #0095F8;
					font-weight: 500;
					font-size: 26rpx;
					color: #FFFFFF;
				}
			}

			.item-bottom {
				font-size: 26rpx;
				color: #999999;
				padding-top: 30rpx;
				border-top: 2rpx solid #EEEEEE;

				.bottom-info {
					margin-bottom: 16rpx;

					.value {
						margin-left: 10rpx;
						color: #333333;
					}
				}
			}
		}

		.student-list-box {
			margin-top: 20rpx;
			background: #fff;
			box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.01);
			border-radius: 24rpx;
			padding: 20rpx;
			max-height: 600rpx;
			overflow-y: auto;
		}

		.student-list {
			max-height: 600rpx;
			overflow-y: auto;

			.student-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx 0;
				border-bottom: 1rpx solid #EEEEEE;

				&:last-child {
					border-bottom: none;
				}

				.student-name {
					font-size: 28rpx;
					color: #333333;
					width: 120rpx;
				}

				.student-no {
					font-size: 26rpx;
					color: #999999;
					margin-left: 10rpx;
					margin-right: 10rpx;
					flex: 1;
				}

				.student-status {
					display: flex;
					align-items: center;

					.tag-box {
						width: 80rpx;
						height: 40rpx;
						border-radius: 6rpx;
						text-align: center;
						line-height: 40rpx;
						font-weight: 400;
						font-size: 22rpx;
						margin-left: 4rpx;
						background: #F6F6F6;
						color: #606060;
						border: 1rpx solid rgba(0, 0, 0, 0.1);
					}

					.late {
						background: #FFF3ED;
						border: 1rpx solid #FE4F04;
						color: #FE4F04;
					}

					.early {
						background: #FAF1FF;
						border: 1rpx solid #CF77FF;
						color: #CF77FF;
					}

					.absent {
						background: #FFF9ED;
						border: 1rpx solid #F8A500;
						color: #F8A500;
					}

					.success {
						background: #F3FFED;
						border: 1rpx solid #4ED142;
						color: #53D347;
					}
				}
			}
		}



		.upload-box {
			margin-top: 20rpx;
			background: #fff;
			border-radius: 24rpx;
			padding: 32rpx;

			.upload-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 20rpx;

				.upload-title {
					font-size: 26rpx;
					color: #333333;
					font-weight: bold;
				}

				.upload-desc {
					font-size: 24rpx;
					color: #999999;
				}
			}
		}

		.submit-box {
			margin-top: 44rpx;
			height: 88rpx;
			background: #0095F8;
			box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 149, 248, 0.5);
			border-radius: 44rpx;
			line-height: 88rpx;
			text-align: center;
			font-size: 30rpx;
			color: #FFFFFF;
		}
	}
</style>